package com.mjh.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import com.mjh.bean.ArticleDetail;
import com.mjh.comm.DBInstance;

public class ArticleDetailDAO {

	public static Logger log = Logger.getLogger(ArticleDetailDAO.class);
	
	public static final String NAME_SPACE = "com.mjh.dao.mapper.articleDetail";
	
	public int insert(ArticleDetail articleDetail){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.insert(NAME_SPACE + ".insert", articleDetail);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}
	
	public int update(ArticleDetail articleDetail){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.update(NAME_SPACE + ".update", articleDetail);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;
	}

	public int delete(int id){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.delete(NAME_SPACE + ".delete", id);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;		
	}

	public int deleteByArticle(int articleId){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return session.delete(NAME_SPACE + ".deleteByArticle", articleId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.commit();
			session.close();
		}
		return 0;		
	}
	
	@SuppressWarnings("unchecked") 
	public List<ArticleDetail> selectForPage(Map<String, Object> par){
		SqlSession session = DBInstance.getInstance().getSession();
		List<ArticleDetail> details = new ArrayList<ArticleDetail>(1);
		try {
			return  session.selectList(NAME_SPACE + ".selectForPage", par);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return details;
	}

	@SuppressWarnings("unchecked") 
	public List<ArticleDetail> selectByArticleId(int articleId){
		SqlSession session = DBInstance.getInstance().getSession();
		List<ArticleDetail> details = new ArrayList<ArticleDetail>(1);
		try {
			return  session.selectList(NAME_SPACE + ".selectByArticle", articleId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return details;
	}
	
	public int selectCount(int articleId){
		SqlSession session = DBInstance.getInstance().getSession();
		try {
			return  (Integer)session.selectOne(NAME_SPACE + ".selectCount", articleId);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return 0;
	}
	
	@SuppressWarnings("unchecked")    
	public List<ArticleDetail> selectByArticleIds(List<Integer> ids){
		SqlSession session = DBInstance.getInstance().getSession();
		List<ArticleDetail> details = new ArrayList<ArticleDetail>(1);
		try {
			return  session.selectList(NAME_SPACE + ".selectByArticleIds", ids);
		} catch (Exception e) {
			log.error(e.getMessage(), e.fillInStackTrace());
		} finally {
			session.close();
		}
		return details;
	}
}
